package flca.mda.api.util;

import java.lang.reflect.Field;
import java.util.Properties;

public class DojoUtils {

//	private final String BASE_TAB = "\t\t\t\t";
//	private final String Q = "\"";	
	
	private NameUtils nu = new NameUtils();
	private TypeUtils tu = new DojoTypeUtils();
//	private TemplateUtils tplu = new TemplateUtils();
	private ValidatorUtils vu = new ValidatorUtils();

	/**
	 * This method must be called at the beginning of the jet
	 */
	public DojoUtils() 
	{
	}
	

	public String getDefaultValue(Field aField) 
	{
		String result = null;
		if (tu.isPrimitive(aField) || tu.isSimpleField(aField)) {
			result = nu.getIniFileValue("dojo.default-value", tu.getTypeName(aField));
		} else {
			result = "null";
		}
		
		if (result == null) {
			result = "null";
		}
		return result;
	}
	
	public String getLabel(Field aField) 
	{
		return aField.getName(); //TODO Label annotation
	}

	private String DONT_SHOW_PROPS[] = new String[] {
		"id", "parent", "createdAt", "createdBy", "modifiedAt", 
		"modifiedBy", "version", "locked", "lockedBy", "lockedAt",
	};

	/**
	 * Not all properties should be visible on the forms
	 * @param property
	 * @return
	 */
	public boolean dontShow(Field aField) {
		for (String s : DONT_SHOW_PROPS) {
			if (s.equals(aField.getName())) {
				return true;
			}
		}
		return false;
	}
	
	public String getExtendsAndImplements() 
	{
		return ""; //TODO
	}
	
	public String getExtendsAndImplementsLitteral(Class<?> uml2Class) 
	{
		return ""; //TODO
	}
	
	
	/**

	 * @param aField
	 * @return
	 */
	public Properties getSubsWithsForClass(Field aField)
	{
		//this will take care of the import
		tu.getTypeName(aField);
		
		Properties result = new Properties();
		result.put("uncapName", nu.uncapName(aField));
		result.put("capName", nu.capName(aField));
		result.put("type", tu.getTypeName(aField));
		result.put("defValue", getDefaultValue(aField));
		return result;
	}
	
	/**
	 * @param aField
	 * @return
	 */
	public Properties getSubsWithsForDojo(Field aField)
	{
		Properties result = new Properties();
		result.put("uncapName", nu.uncapName(aField));
		result.put("capName", nu.capName(aField));
		result.put("inputTag", getInputTag(aField));
		result.put("label", getLabel(aField));
		result.put("cssClassname", getCssClassname(aField));
		result.put("maxchars", getMaxChars(aField));
		result.put("required", getRequired(aField));
		result.put("enumClass", tu.getTypeName(aField));
		return result;
	}

//	/**
//	 * @param aField
//	 * @return
//	 */
//	public Properties getSubsWithsForFlexCrudOper(CrudMethod aCrudMethod)
//	{
//		Class<?> crudent = aCrudMethod.getEntity(); 
//		String entityName = tplu.getClassName(crudent, Tid.ENTITY.name()); 
//		String pktype = tu.getIdTypeName(crudent);
//		
//		Properties result = new Properties();
//		result.put("entity", entityName);
//		result.put("pkType", pktype);
//		return result;
//	}


	/**
	 * note Enum, One2Manu and Many2One are filtered out before this step
	 * @param aField
	 * @return
	 */
	public String getInputTag(Field aField)
	{
		String result = "TextBox" ;
		if (isRequired(aField)) {
			result = "ValidationTextBox";
		}
		
		if (tu.isNumeric(aField)) {
			result = "NumberTextBox";
		} else 	if (tu.isDate(aField.getType())) {
			result = "DateTextBox";
		} else if (tu.isTime(aField.getType())) {
			result = "TimeTextBox";
		} else if (tu.isBoolean(aField.getType())) {
			result = "CheckBox";
		}
		return result;
	}

	public String getCssClassname(Field aField)
	{
		return (isRequired(aField)) ? "classname : 'req'" : "classname : 'normal'";
	}

	public boolean isRequired(Field aField)
	{
		if (vu.mustValidate(aField)) {
			if (vu.isRequired(aField)) {
				return true;
			}
		}
		
		return false;
	}
	
	public String getRequired(Field aField)
	{
		return (isRequired(aField)) ? "true" : "false";
	}

	
	public String getMaxChars(Field aField)
	{
		return "";
	}

	public boolean isJoinTable() 
	{
		return false;
	}
	
	public JoinTableProps[] getJoinTableProps()
	{
		return new JoinTableProps[] {new JoinTableProps(), new JoinTableProps()} ;
	}
	

}